from django.contrib.auth import get_user_model
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings  # 用于自定义用户模型

#在线编辑简历
class Resume(models.Model):
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE, related_name='resumes')
    name = models.CharField('姓名', max_length=50)
    gender = models.CharField('性别', max_length=10, blank=True)
    birthday = models.DateField('出生日期', null=True, blank=True)
    phone = models.CharField('电话', max_length=20, blank=True)
    email = models.EmailField('邮箱', blank=True)
    city = models.CharField('城市', max_length=50, blank=True)
    self_intro = models.TextField('自我介绍', blank=True)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'resume'

    def __str__(self):
        return f"{self.name}({self.user.username})"


#教育经历
class Education(models.Model):
    resume = models.ForeignKey(Resume, on_delete=models.CASCADE, related_name='educations')
    school = models.CharField('学校', max_length=100)
    degree = models.CharField('学历', max_length=50)
    major = models.CharField('专业', max_length=100)
    start_date = models.DateField('开始时间')
    end_date = models.DateField('结束时间', null=True, blank=True)
    desc = models.TextField('描述', blank=True)
    class Meta:
        db_table = 'resume_education'


#工作经历
class WorkExperience(models.Model):
    resume = models.ForeignKey(Resume, on_delete=models.CASCADE, related_name='work_experiences')
    company = models.CharField('公司', max_length=100)
    position = models.CharField('职位', max_length=100)
    start_date = models.DateField('开始时间')
    end_date = models.DateField('结束时间', null=True, blank=True)
    desc = models.TextField('工作内容', blank=True)
    class Meta:
        db_table = 'resume_work_experience'





#简历模板
from django.db import models

class ResumeTemplate(models.Model):
    name = models.CharField('模板名称', max_length=100)
    preview_img = models.URLField('预览图片', blank=True)  # 可存爬虫采集的图片URL
    download_url = models.URLField('模板下载链接', blank=True)  # 可存爬虫采集的下载地址
    source = models.CharField('来源网站', max_length=100, blank=True)  # 如"500丁"
    created_at = models.DateTimeField('采集时间', auto_now_add=True)
    updated_at = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'resume_template'


# 简历分析结果模型
User = get_user_model()

class ResumeAnalysis(models.Model):
    STATUS_CHOICES = [
        ('pending', '分析中'),
        ('completed', '已完成'),
        ('failed', '分析失败'),
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='resume_analyses')
    resume_file = models.FileField('简历文件', upload_to='resume_analysis/')
    original_filename = models.CharField('原始文件名', max_length=255)
    
    # 分析结果
    extracted_text = models.TextField('提取的文本内容', blank=True)
    analysis_result = models.JSONField('分析结果', default=dict)
    improvement_suggestions = models.JSONField('改进建议', default=list)
    recommended_positions = models.JSONField('推荐职位', default=list)
    recommended_companies = models.JSONField('推荐公司', default=list)
    
    # 状态和时间
    status = models.CharField('分析状态', max_length=20, choices=STATUS_CHOICES, default='pending')
    error_message = models.TextField('错误信息', blank=True)
    created_at = models.DateTimeField('创建时间', auto_now_add=True)
    completed_at = models.DateTimeField('完成时间', null=True, blank=True)
    
    class Meta:
        db_table = 'resume_analysis'
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.user.username if self.user else '匿名'} - {self.original_filename}"


# 职位推荐模型
class JobRecommendation(models.Model):
    analysis = models.ForeignKey(ResumeAnalysis, on_delete=models.CASCADE, related_name='job_recommendations')
    position_title = models.CharField('职位名称', max_length=200)
    company_name = models.CharField('公司名称', max_length=200)
    salary_range = models.CharField('薪资范围', max_length=100, blank=True)
    location = models.CharField('工作地点', max_length=100, blank=True)
    requirements = models.TextField('职位要求', blank=True)
    match_score = models.FloatField('匹配度', default=0.0)
    reason = models.TextField('推荐理由', blank=True)
    
    class Meta:
        db_table = 'job_recommendation'
    
    def __str__(self):
        return f"{self.position_title} at {self.company_name}"


# 公司推荐模型
class CompanyRecommendation(models.Model):
    analysis = models.ForeignKey(ResumeAnalysis, on_delete=models.CASCADE, related_name='company_recommendations')
    company_name = models.CharField('公司名称', max_length=200)
    industry = models.CharField('行业', max_length=100, blank=True)
    company_size = models.CharField('公司规模', max_length=50, blank=True)
    location = models.CharField('公司地址', max_length=200, blank=True)
    description = models.TextField('公司描述', blank=True)
    match_score = models.FloatField('匹配度', default=0.0)
    reason = models.TextField('推荐理由', blank=True)
    
    class Meta:
        db_table = 'company_recommendation'
    
    def __str__(self):
        return f"{self.company_name} ({self.industry})"








